const { getAll, add, update, remove } = require('../../utils/dbTools')

const { getUploadedUrl } = require('../../utils/upload')

const { isValidator2 } = require('../../utils/util')

const tableName = 'posts'

/**
 * 获取文章列表
 * @param {Number} page 页码
 * @param {Number} size 数据条数
*/
const postsList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query
    // 获取登录账号id
    const { id: author_id } = req.adminLoginInfo
    if (!page || !size) {
        output.code = 500
        output.msg = "参数错误"
        return res.send(output)
    }
    const result = await getAll(tableName, { author_id, page, size })
    if (!result) {
        output.code = 500
        output.msg = "操作失败"
        return res.send(output)
    }
    output.data = result
    res.send(output)
}

/**
 * 新增文章
 * @param {String} title 标题
 * @param {Number} cid 分类id
 * @param {Number} top 是否置顶0,1
 * @param {File} img 封面图
 * @param {String} content 内容
*/

const postsAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { title, cid, content, top } = req.body
    const { file } = req
    if (!title || !cid || !content || !top) {
        output.code = 500
        output.msg = "参数错误"
        return res.send(output)
    }
    // 检测文章名称是否重复
    const valid = await isValidator2(tableName, 'title', title)
    if (!valid) {
        output.code = 500
        output.msg = '数据已存在,不能重复添加'
        return res.send(output)
    }
    // 获取登录账号id
    const { id: author_id, bus_id } = req.adminLoginInfo
    const data = { title, cid, content, top, author_id, bus_id }
    // 获取图片地址
    if (file) data.cover = getUploadedUrl(file)
    const result = await add(data, tableName)
    if (!result) {
        output.code = 500
        output.msg = "操作失败"
        return res.send(output)
    }
    res.send(output)
}


/**
 * 新增文章
 * @param {Number} id 文章id
 * @param {String} title 标题
 * @param {Number} cid 分类id
 * @param {Number} top 是否置顶0,1
 * @param {File} img 封面图
 * @param {String} content 内容
*/

const postsUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id, title, cid, content, top } = req.body
    const { file } = req
    if (!id) {
        output.code = 500
        output.msg = "参数错误"
        return res.send(output)
    }
    const data = {}
    if (title) data.title = title
    if (cid) data.cid = cid
    if (content) data.content = content
    if (top) data.top = top
    // 获取图片地址
    if (file) data.cover = getUploadedUrl(file)
    const result = await update(id, data, tableName)
    if (!result) {
        output.code = 500
        output.msg = "操作失败"
        return res.send(output)
    }
    res.send(output)
}

/**
 * 删除文章
 * @param {number} id 文章id
 * 
*/
const postsRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query
    if (!id) {
        output.code = 500
        output.msg = "参数错误"
        return res.send(output)
    }
    const result = await remove(id, tableName)
    if (!result) {
        output.code = 500
        output.msg = "操作失败"
        return res.send(output)
    }
    res.send(output)
}
module.exports = {
    postsList,
    postsAdd,
    postsUpdate,
    postsRemove
}